package com.baoyang.config;

import com.baoyang.tool.api.R;
import com.baoyang.tool.api.ResultCode;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public R handleRuntimeException(RuntimeException ex) {
        // 返回自定义的错误信息
        return R.fail(ex.getMessage());
    }

    /**
     * 处理认证异常
     */
    @ExceptionHandler(AuthenticationException.class)
    public R handleAuthenticationException(AuthenticationException ex) {
        return R.fail(ResultCode.UN_AUTHORIZED, "认证失败：" + ex.getMessage());
    }

    /**
     * 处理权限不足异常
     */
    @ExceptionHandler(AccessDeniedException.class)
    public R handleAccessDeniedException(AccessDeniedException ex) {
        return R.fail(ResultCode.REQ_REJECT, "权限不足：" + ex.getMessage());
    }

    // 你也可以加其他异常类型的处理方法
}